/*
 * Created by JFormDesigner on Tue Jul 02 03:03:45 CST 2024
 */

package scct.hys.ui;

import scct.hys.dao.HallDao;
import scct.hys.dao.ScreeningDao;
import scct.hys.dao.UserDao;
import scct.hys.model.Screening;
import scct.hys.model.User;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.Objects;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author 26984
 */
public class PanelScreening extends JPanel {
    FormMain main;
    DefaultTableModel model;
    DefaultTableModel Searchmodel=null;
    int id;
    public PanelScreening(FormMain main) {
        initComponents();
        this.id=id;
        initView();
    }

    private void initView() {
        model = (DefaultTableModel) tabScreen.getModel();
        model.addColumn("id");
        model.addColumn("电影名称");
        model.addColumn("上映时间");
        model.addColumn("票价");
        model.addColumn("电影类型");
        getTable();


    }

    private void getTable() {
        model.setRowCount(0);
        ScreeningDao screeningDao=new ScreeningDao();
        List<Screening> allScreenings= screeningDao.getAllScreenings();
        for(Screening allScreening:allScreenings){
            model.addRow(new Object[]{allScreening.getId(),allScreening.getMovieid(),allScreening.getStartTime(),allScreening.getPrice(),allScreening.getType()});
        }
        System.out.println(allScreenings);
    }

    private void btnAdd(ActionEvent e) {
        // TODO add your code here
        DlgScreeningAdd dlgScreeningAdd=new DlgScreeningAdd(main,id);
        dlgScreeningAdd.setVisible(true);
        dlgScreeningAdd.setModal(true);
        getTable();
    }

    private void btnUpdata(ActionEvent e) {
        // TODO add your code here

        if (tabScreen.getSelectedRowCount()==0){
            JOptionPane.showMessageDialog(this,"请选择修改对象");
            return;
        }
        int index=tabScreen.getSelectedRow();
        int id=Integer.parseInt(model.getValueAt(index,0).toString());
        System.out.println(id);
        DlgScreeningUpdate dlgScreeningUpdate=new DlgScreeningUpdate(main,id);
        dlgScreeningUpdate.setModal(true);
        dlgScreeningUpdate.setVisible(true);
        getTable();
    }

    private void btnCancel(ActionEvent e) {
        // TODO add your code here
        if (tabScreen.getSelectedRowCount()==0){
            JOptionPane.showMessageDialog(this,"删除成功");
            return;
        }
        int index=tabScreen.getSelectedRow();
        int id=Integer.parseInt(model.getValueAt(index,0).toString());
        ScreeningDao screeningDao=new ScreeningDao();
        screeningDao.delScreening(id);
        getTable();
    }

    private void btnsearch(ActionEvent e) {
        // TODO add your code here
        tabScreen.setModel(model);
        Searchmodel =new DefaultTableModel();
        Searchmodel.addColumn("id");
        Searchmodel.addColumn("电影名称");
        Searchmodel.addColumn("上映时间");
        Searchmodel.addColumn("票价");
        Searchmodel.addColumn("电影类型");
        Searchmodel.setRowCount(0);
        String searchkey = txtSearchkey.getText();
        System.out.println(searchkey);
        String searchlist = (String) cboScreening.getSelectedItem();
        System.out.println(searchlist);
        int row = tabScreen.getRowCount();
        System.out.println("row:" + row);
        int column = tabScreen.getColumnCount();
        System.out.println("column" + column);
        for (int i = 0; i < row; i++) {
            if (tabScreen.getRowCount() < row) {
            } else {

                for (int j = 0; j < column; j++) {
                    if (Objects.equals(searchlist, tabScreen.getColumnName(j))) {
                        System.out.println(0002);
                        System.out.println("单元格的值:" + tabScreen.getValueAt(i, j));
                        String cellvalue = tabScreen.getValueAt(i, j).toString().toLowerCase();
                        System.out.println(cellvalue.contains(searchkey));
                        if (cellvalue.contains(searchkey)) {

                            Object[] rowDate = new Object[tabScreen.getColumnCount()];
                            for (int k = 0; k < tabScreen.getColumnCount(); k++) {
                                rowDate[k] = tabScreen.getValueAt(i, k);
                                System.out.println(rowDate[k]);
                            }
                            Searchmodel.addRow(rowDate);
                            tabScreen.setRowSelectionAllowed(true);
                            break;
                        }
                    }

                }
            }

        }
        tabScreen.setModel(Searchmodel);
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - 韩月生
        scrollPane2 = new JScrollPane();
        panel1 = new JPanel();
        btnAdd = new JButton();
        btnCancel = new JButton();
        btnUpdata = new JButton();
        cboScreening = new JComboBox<>();
        txtSearchkey = new JTextField();
        btnsearch = new JButton();
        scrollPane3 = new JScrollPane();
        tabScreen = new JTable();

        //======== this ========
        setBorder ( new javax . swing. border .CompoundBorder ( new javax . swing. border .TitledBorder ( new javax . swing
        . border .EmptyBorder ( 0, 0 ,0 , 0) ,  "JF\u006frmD\u0065sig\u006eer \u0045val\u0075ati\u006fn" , javax. swing .border . TitledBorder
        . CENTER ,javax . swing. border .TitledBorder . BOTTOM, new java. awt .Font ( "Dia\u006cog", java .
        awt . Font. BOLD ,12 ) ,java . awt. Color .red ) , getBorder () ) )
        ;  addPropertyChangeListener( new java. beans .PropertyChangeListener ( ){ @Override public void propertyChange (java . beans. PropertyChangeEvent e
        ) { if( "\u0062ord\u0065r" .equals ( e. getPropertyName () ) )throw new RuntimeException( ) ;} } )
        ;
        setLayout(new BorderLayout());

        //======== scrollPane2 ========
        {

            //======== panel1 ========
            {
                panel1.setLayout(new FlowLayout(FlowLayout.LEFT, 25, 5));

                //---- btnAdd ----
                btnAdd.setText("\u589e\u52a0\u653e\u6620\u8ba1\u5212");
                btnAdd.addActionListener(e -> btnAdd(e));
                panel1.add(btnAdd);

                //---- btnCancel ----
                btnCancel.setText("\u5220\u9664");
                btnCancel.addActionListener(e -> btnCancel(e));
                panel1.add(btnCancel);

                //---- btnUpdata ----
                btnUpdata.setText("\u4fee\u6539");
                btnUpdata.addActionListener(e -> btnUpdata(e));
                panel1.add(btnUpdata);

                //---- cboScreening ----
                cboScreening.setModel(new DefaultComboBoxModel<>(new String[] {
                    "id",
                    "\u7535\u5f71\u540d\u79f0",
                    "\u4e0a\u6620\u65f6\u95f4",
                    "\u7968\u4ef7",
                    "\u7535\u5f71\u7c7b\u578b"
                }));
                cboScreening.setMaximumRowCount(4);
                panel1.add(cboScreening);

                //---- txtSearchkey ----
                txtSearchkey.setColumns(6);
                panel1.add(txtSearchkey);

                //---- btnsearch ----
                btnsearch.setText("\u67e5\u627e");
                btnsearch.addActionListener(e -> btnsearch(e));
                panel1.add(btnsearch);
            }
            scrollPane2.setViewportView(panel1);
        }
        add(scrollPane2, BorderLayout.NORTH);

        //======== scrollPane3 ========
        {
            scrollPane3.setViewportView(tabScreen);
        }
        add(scrollPane3, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - 韩月生
    private JScrollPane scrollPane2;
    private JPanel panel1;
    private JButton btnAdd;
    private JButton btnCancel;
    private JButton btnUpdata;
    private JComboBox<String> cboScreening;
    private JTextField txtSearchkey;
    private JButton btnsearch;
    private JScrollPane scrollPane3;
    private JTable tabScreen;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
